package com.fsh.subject.application.intercepter;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fsh.subject.common.context.SubjectContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SubjectIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 取出 loginId
        String loginId = request.getHeader("loginId");
        if(StringUtils.isNotBlank(loginId)){
            // 存入ThreadLocal 中
            SubjectContextHolder.set("loginId",loginId);
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        SubjectContextHolder.remove();
    }
}
